package com.example.tengxunapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.entity.ChinaTotal;
import com.example.entity.NocvData;
import com.example.service.ChinaTotalService;
import com.example.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

@Component
public class ChinaTotalScheduleTask {

    @Autowired
    ChinaTotalService chinaTotalService;

    @Autowired
    IndexService indexService;


    @Scheduled(fixedDelay = 1, timeUnit = TimeUnit.DAYS) //1天拿一次
    public void updateChinaTotalToDB() throws Exception {

/**
 [腾讯实时疫情](https://news.qq.com/zt2020/page/feiyan.htm#/global)
 网易数据接口：**https://c.m.163.com/ug/api/wuhan/app/data/list-total
 腾讯数据接口：**https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5
 {
 "reqId":16602249715717572,
 "code":10000,
 "msg":"成功",
 "data":{
 "chinaTotal":{
 "today":{
 "confirm":25494,
 "suspect":null,
 "heal":371,
 "dead":29,
 "severe":null,
 "storeConfirm":25094,
 "input":86
 },
 "total":{
 "confirm":5398455,
 "suspect":0,
 "heal":308189,
 "dead":24084,
 "severe":0,
 "input":21327
 },
 "extData":{
 "noSymptom":6263,
 "incrNoSymptom":1466
 }
 },
 "lastUpdateTime":"2022-08-11 18:41:49",
 "overseaLastUpdateTime":"2022-08-11 21:30:37",
 "areaTree":[
 {
 "today":{
 "confirm":24264,
 "suspect":null,
 "heal":315,
 "dead":45,
 "severe":null,
 "storeConfirm":23904,
 "input":56
 },
 "total":{
 "confirm":5422523,
 "suspect":0,
 "heal":308504,
 "dead":24129,
 "severe":0,
 "input":21383
 },
 "extData":{
 "noSymptom":7088,
 "incrNoSymptom":1305
 },
 "name":"中国",
 "id":"0",
 "lastUpdateTime":"2022-08-12 09:26:39",
 "children":{
 "today":{
 "confirm":0,
 "suspect":null,
 "heal":1,
 "dead":0,
 "severe":null,
 "storeConfirm":-1
 },
 "total":{
 "confirm":68410,
 "suspect":0,
 "heal":63895,
 "dead":4512,
 "severe":0,
 "input":0
 },
 "extData":{

 },
 "name":"湖北",
 "id":"420000",
 "lastUpdateTime":"2022-08-12 11:22:45",
 "children":[
 {
 "today":{
 "confirm":0,
 "suspect":null,
 "heal":null,
 "dead":null,
 "severe":null,
 "storeConfirm":null
 },
 "total":{
 "confirm":50424,
 "suspect":0,
 "heal":46555,
 "dead":3869,
 "severe":0
 },
 "extData":{

 },
 "name":"武汉",
 "id":"420100",
 "lastUpdateTime":"2022-08-12 00:00:08",
 "children":[

 ]
 },
 {
 "today":{
 "confirm":0,
 "suspect":null,
 "heal":null,
 "dead":null,
 "severe":null,
 "storeConfirm":null
 },
 "total":{
 "confirm":3518,
 "suspect":0,
 "heal":3389,
 "dead":129,
 "severe":0
 },
 "extData":{

 },
 "name":"孝感",
 "id":"420900",
 "lastUpdateTime":"2022-08-12 00:00:07",
 "children":[

 ]
 }
 ]
 }
 }
 ]
 }
 }
 **/

        TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8");
        TimeZone.setDefault(tz);

        //拿到原始数据
        HttpUtils httpUtils = new HttpUtils();
        String string = httpUtils.getData();
        //拿到data
        JSONObject jsonObject = JSONObject.parseObject(string);
        Object data = jsonObject.get("data");
        //拿到chinaTotal
        JSONObject jsonObjectData = JSONObject.parseObject(data.toString());
        Object chinaTotal = jsonObjectData.get("chinaTotal");
        Object lastUpdateTime = jsonObjectData.get("lastUpdateTime");
        //拿到total
        JSONObject jsonObjectChinaTotal = JSONObject.parseObject(chinaTotal.toString());
        Object total = jsonObjectChinaTotal.get("total");
//        System.out.println(total.toString());
        //拿到各个数据
        JSONObject jsonObjectTotal = JSONObject.parseObject(total.toString());
        Object confirm = jsonObjectTotal.get("confirm");
        Object input = jsonObjectTotal.get("input");
        Object severe = jsonObjectTotal.get("severe");
        Object heal = jsonObjectTotal.get("heal");
        Object dead = jsonObjectTotal.get("dead");
        Object suspect = jsonObjectTotal.get("suspect");
        //为程序实体赋值
        ChinaTotal dataEntity = new ChinaTotal();
        dataEntity.setConfirm((Integer) confirm);
        dataEntity.setDead((Integer) dead);
        dataEntity.setHeal((Integer) heal);
        dataEntity.setInput((Integer) input);
        dataEntity.setSevere((Integer) severe);
        dataEntity.setSuspect((Integer) suspect);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dataEntity.setUpdateTime(format.parse(String.valueOf(lastUpdateTime)));
        //插入数据库

        chinaTotalService.save(dataEntity);



        //拿到areaTree
        JSONArray areaTree = jsonObjectData.getJSONArray("areaTree");
        Object[] objects = areaTree.toArray();
        // 所有国家的名字，中国是第三个，对应的列表下标是2
//        for (int i = 0; i < objects.length; i++) {
//            JSONObject jsonObject1 = JSONObject.parseObject(objects[i].toString());
//            Object name = jsonObject1.get("name");
//            System.out.println(name);
//        }
        //数组中第三个为中国省份数据
        JSONObject jsonObject1 = JSONObject.parseObject(objects[2].toString());
        JSONArray children1 = jsonObject1.getJSONArray("children");
        Object[] objects1 = children1.toArray();

        // 遍历中国地区的数据
        List<NocvData> list = new ArrayList<>();

        for (int i = 0; i < objects1.length; i++) {
            NocvData nocvData = new NocvData();
            JSONObject jsonObject2 = JSONObject.parseObject(objects1[i].toString());
            Object name = jsonObject2.get("name");//省份名称
            Object total2 = jsonObject2.get("total");
            JSONObject totalJson = JSONObject.parseObject(total2.toString());
            Object confirm2 = totalJson.get("confirm");//累计确诊数量
            Object heal2 = totalJson.get("heal");//累计治愈数量
            Object dead2 = totalJson.get("dead");//累计死亡数量

            //目前确诊=累计确诊-累计治愈-累计死亡
            int confirmNow = Integer.parseInt(confirm2.toString())
                    -Integer.parseInt(heal2.toString())
                    -Integer.parseInt(dead2.toString());

//            System.out.println(name+"："+confirm2);
            //获取省份更新的时间
            Object extData = jsonObject2.get("extData");
            JSONObject extDataJson = JSONObject.parseObject(extData.toString());
            Object lastUpdateTime1 = extDataJson.get("lastUpdateTime");
            //封装数据
            nocvData.setName(name.toString());
            nocvData.setValue(confirmNow);
            String s = String.valueOf(lastUpdateTime1);
            if (lastUpdateTime1 == null) {
                nocvData.setUpdateTime(new Date());
            } else {
                nocvData.setUpdateTime(format.parse(s));
            }

            list.add(nocvData);
        }
        // 插入数据库各个省份的数据
        indexService.saveBatch(list);

        //清空缓存
        Jedis jedis=new Jedis("localhost");
        if (jedis!=null){
            jedis.flushDB();

        }
    }
}
